<?php

namespace app\admin\controller;

use app\admin\model\SiteConfig;
use support\Request;
use support\Response;

class Site_config extends Base
{
    /**
     * 网站设置
     * @param  Request  $request
     * @return string|Response
     */
    public function web(Request $request)
    {
        $model = $this->model->where('key', 'web')->field('value')->find();
        if ($request->isAjax()) {
            $model->save(['value' => $request->post()]);
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        return view('site_config/web', [
            'data' => $model['value'],
        ]);
    }

    /**
     * 邮箱设置
     * @param  Request  $request
     * @return string|Response
     */
    public function email(Request $request)
    {
        $model = $this->model->where('key', 'email')->field('value')->find();
        if ($request->isAjax()) {
            $model->save(['value' => $request->post()]);
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        return view('site_config/email', [
            'data' => $model['value'],
        ]);
    }

    /**
     * 上传设置
     * @param  Request  $request
     * @return string|Response
     */
    public function file(Request $request)
    {
        $model = $this->model->where('key', 'file')->field('value')->find();
        if ($request->isAjax()) {
            $model->save(['value' => $request->post()]);
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        return view('site_config/file', [
            'data' => $model['value'],
        ]);
    }

    /**
     * 初始化
     */
    protected function initialize()
    {
        $this->model = new SiteConfig;
    }
}
